package com.szh.mysobproject.util;

import android.annotation.SuppressLint;

import com.shuyu.gsyvideoplayer.utils.AnimatedGifEncoder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import pl.droidsonroids.gif.GifDrawable;
import timber.log.Timber;

public class GifHelper {
    public static final String SLASH = "/";

    /**
     * 先把gif 分解成bitmap，进行减帧操作，最后合成一张新的gif
     * 根据指定文件长度进行压缩
     *
     * @param getFile      gif文件
     * @param targetLength 压缩的目标体积
     * @param savePath     压缩后的保存路径
     * @return
     */
    @SuppressWarnings("all")
    public static String compressFrame(File getFile, long targetLength, String savePath) {
        GifDrawable gifDrawable = null;
        try {
            long getFileLength = getFile.length() / 1024;
            Timber.d("getFileLength ===" + getFileLength);
            gifDrawable = new pl.droidsonroids.gif.GifDrawable(getFile);
            if (getFileLength > targetLength) {
                // 计算压缩比例 例如是2  每2张取1张 3 4 5 6 同理
                int mold = (int) (Math.ceil(getFileLength * 1.0 / targetLength) + 1);
                Timber.d("mold" + mold);
                return compressFrameGif(gifDrawable, mold, savePath);
            }
        } catch (Exception e) {
            Timber.e("compressFrameGif  e = " + e.getMessage());
        } finally {
            if (gifDrawable == null) {
                gifDrawable.recycle();
            }
        }
        return savePath;
    }

    /**
     * 先把GIF分解成bitmap，进行减帧操作，最后合成一张新的GIF
     * 2.根据指定帧数进行压缩
     *
     * @param gifFile   gif文件
     * @param maxFrames 最大帧数
     * @param savePath  压缩后的保存路径
     */
    public static String compressFrame(File gifFile, int maxFrames, String savePath) {
        GifDrawable gifDrawable = null;
        try {
            gifDrawable = new GifDrawable(gifFile);
            int gifFrames = gifDrawable.getNumberOfFrames();
            if (gifFrames > maxFrames) {
                // 计算压缩比例 例如是2  每2张取1张 3 4 5 6 同理

                int mold = (int) (Math.ceil(gifFrames * 1.0 / maxFrames)) + 1;
                return compressFrameGif(gifDrawable, mold, savePath);
            }
        } catch (Exception e) {
            Timber.e("compressFrameGif  e = " + e.getMessage());
        } finally {
            if (gifDrawable != null) {
                gifDrawable.recycle();
            }
        }
        return savePath;
    }

    //写入保存路径， 抽离方法
    private static String compressFrameGif(GifDrawable gifDrawable, int mold, String savePath) {
        ByteArrayOutputStream bos = null;
        FileOutputStream outStream = null;
        try {
            int gifFrame = gifDrawable.getNumberOfFrames();
            int gifDuration = gifDrawable.getDuration();
            bos = new ByteArrayOutputStream();
            AnimatedGifEncoder encoder = new AnimatedGifEncoder();
            encoder.setRepeat(0);
            encoder.start(bos);
            encoder.setDelay(gifDuration / gifFrame);
            //核心内容 抽帧，并添加帧进文件夹
            for (int i = 0; i < gifFrame; i++) {
                // 这里需要各自去定制 本质上抽出来的帧数的大小都会比原图帧的大 如何缩减达到减少内存的效果
                if (i % mold == 0) {
                    encoder.addFrame(gifDrawable.seekToFrameAndGet(i));
                }
            }
            encoder.finish();
            outStream = new FileOutputStream(savePath);
            outStream.write(bos.toByteArray());
        } catch (Exception e) {
            Timber.e(e);
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (outStream != null) {
                    outStream.close();
                }
                return savePath;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return savePath;
    }
}
